﻿using System;
using System.Collections.Generic;
using System.Text;
using NATUPNPLib;
using System.Net;
using System.Threading;

namespace MyPhotoIndex.Utilities.Network_security
{
    // This required a reference to the COM NATUPnP 1.0 Type Library to be added to the project.
    public static class UPnPNATHelper
    {
        private static NATUPNPLib.UPnPNATClass _UPnPNat = null;
        private static NATUPNPLib.UPnPNATClass UPnPNat
        {
            get
            {
                if (_UPnPNat == null)
                {
                    _UPnPNat = new NATUPNPLib.UPnPNATClass();
                }
                return _UPnPNat;
            }
        }

        /// <summary>
        /// Gets a IStaticPortMappingCollection that contains all the current IStaticPortMapping's
        /// </summary>
        private static NATUPNPLib.IStaticPortMappingCollection StaticPortMappings
        {
            get
            {
                return UPnPNat.StaticPortMappingCollection;
            }
        }


        /// <summary>
        /// Adds a new Static Port Mapping
        /// </summary>
        /// <param name="externalPort">
        /// Specifies the external port for this port mapping.
        /// </param>
        /// <param name="protocol">
        /// Specifies the protocol. This parameter should be either UDP or TCP.
        /// </param>
        /// <param name="internalPort">
        /// Specifies the internal port for this port mapping.
        /// </param>
        /// <param name="internalClient">
        /// Specifies the name of the client on the private network that uses this port mapping.
        /// </param>
        /// <param name="enabled">
        /// Specifies whether the port is enabled.
        /// </param>
        /// <param name="description">
        /// Specifies a description for this port mapping.
        /// </param>
        /// <returns>The IStaticPortMapping that was added.</returns>
        public static bool Add(
            int externalPort,
            string protocol,
            int internalPort,
            string internalClient,
            bool enabled,
            string description
            )
        {
            if (UPnPNat.StaticPortMappingCollection == null)
            {
                Logger.Log("Could not find a configurable UPnP device");
                return false;
            }

            if (string.IsNullOrEmpty(protocol))
            {
                throw new ArgumentException("Protocol must be specified. Accepted values are UPD or TCP.", "protocol");
            }
            else if (!(protocol.ToLower() == "udp" || protocol.ToLower() == "tcp"))
            {
                throw new ArgumentException("Invalid Protocol. Accepted values are UPD or TCP.", "protocol");
            }

            if (string.IsNullOrEmpty(internalClient))
            {
                throw new ArgumentException("Internal Client must be specified.", "internalClient");
            }

            NATUPNPLib.IStaticPortMapping newMapping = UPnPNat.StaticPortMappingCollection.Add(externalPort, protocol, internalPort, internalClient, enabled, description);
            
            return (newMapping != null);
        }

        /// <summary>
        /// Removes an existing Static Port Mapping
        /// </summary>
        /// <param name="externalPort">
        /// Specifies the external port for this port mapping.
        /// </param>
        /// <param name="protocol">
        /// Specifies the protocol. This parameter should be either UDP or TCP.
        /// </param>
        public static void Remove(
            int externalPort,
            string protocol
            )
        {
            try
            {
                if (string.IsNullOrEmpty(protocol))
                {
                    throw new ArgumentException("Protocol must be specified. Accepted values are UPD or TCP.", "protocol");
                }
                else if (!(protocol.ToLower() == "udp" || protocol.ToLower() == "tcp"))
                {
                    throw new ArgumentException("Invalid Protocol. Accepted values are UPD or TCP.", "protocol");
                }
                if (UPnPNat.StaticPortMappingCollection != null)
                {
                    UPnPNat.StaticPortMappingCollection.Remove(externalPort, protocol);
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
            }
        }
    }
}
